<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%java.util.Random r=new java.util.Random();  %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
<!--
	function checkForm(){
		var curAssignPeriod = document.all.assignPeriod.value;
		if(curAssignPeriod == 0){
			alert("<fmt:message key='target.message.assignPeriod'/>");
			return false;
		}
		
		var objs = window.frames['targetFrame'].document.getElementsByTagName("input");
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value == ""){
				alert("<fmt:message key='target.message.emptyTarget'/>");
				return false;
			}
		}
		
		var objs = document.getElementsByName("deptId");
		var sign = 0;
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value != "")sign = sign + 1;
		}
		if(sign <= 0){
			alert("<fmt:message key='target.message.assignDept'/>");
			return false;
		}
		
		return true;
	}

	function checkDept(){
		var objs = document.getElementsByName("deptId");
		var sign = 0;
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value != "")sign = sign + 1;
		}
		if(sign <= 0){
			alert("<fmt:message key='target.message.assignDept'/>");
			return false;
		}
		return true;
	}
	
	function back(){
		window.location.href = "./businessTarget.do";
		window.location.target = "main";
	}
	
	function getDeptId(){
		var objs = document.getElementsByName("deptId");
		var deptIds = "";
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value != "")deptIds = deptIds + objs[i].value + ",";
		}
		if(deptIds == ""){
			deptIds = "<c:out value='${deptIds}'/>";
		}
		return deptIds;
	}

	function displayTable(obj){
		//alert(obj.value);
		//var deptIds = "<c:out value='${deptIds}'/>";
		var assignPeriod = obj.value;
		if(checkDept() && assignPeriod != 0){
			document.updateTargetForm.action="./businessTarget.do?action=selectDept&deptIds="+getDeptId()+"&assignPeriod="+assignPeriod;
			document.updateTargetForm.target="main";
			document.updateTargetForm.submit();
			/**if(obj.value == 1){
				document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateMonthTarget&deptIds="+getDeptId();
			}else if(obj.value == 2){
				document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateSeasonTarget&deptIds="+getDeptId();
			}else if(obj.value == 3){
				document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateYearTarget&deptIds="+getDeptId();
			}*/
		}
	}
	
	function deptDialog(){
		var url = "./businessTarget.do?action=preSelectDept&radom="+<%=r.nextInt(100)%>;
		var dlgParam = "dialogHeight:350px;dialogWidth:300px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		
		if(rt != undefined){
			//根据从子窗口得到部门ids字符串
			
			var curAssignPeriod = document.all.assignPeriod.value;
			if(curAssignPeriod == 0){
				alert("<fmt:message key='target.message.assignPeriod'/>");
			}else{
				//document.getElementById("deptIds").value = rt;
				document.updateTargetForm.action="./businessTarget.do?action=selectDept&deptIds="+rt;
				document.updateTargetForm.target="main";
				document.updateTargetForm.submit();
				/**if(curAssignPeriod == 1){
					document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateMonthTarget&deptIds="+rt;
				}else if(curAssignPeriod == 2){
					document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateSeasonTarget&deptIds="+rt;
				}else if(curAssignPeriod == 3){
					document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateYearTarget&deptIds="+rt;
				}*/
			}
		}
	}
	
	function save(){
		if(checkForm()){
			//var deptIds = "<c:out value='${deptIds}'/>";
			var objs = document.getElementsByName("deptId");
			var deptIds = "";
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != "")deptIds = deptIds + objs[i].value + ",";
			}
			
			var curAssignPeriod = document.all.assignPeriod.value;
			window.frames['targetFrame'].document.updateMonthTarget.action="./businessTarget.do?action=updateTarget&deptIds="+deptIds+"&assignPeriod="+curAssignPeriod;
			window.frames['targetFrame'].document.updateMonthTarget.target="main";
			window.frames['targetFrame'].document.updateMonthTarget.submit();
		}
	}
	
	function initPage(){
		//document.getElementById("curTarget").innerText = "<c:out value='${businessTarget.totalTarget}'/>";
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.addSuccess'/>");
		}
		
		//var deptIds = "<c:out value='${deptIds}'/>";
		if("<c:out value='${businessTarget.assignPeriod}'/>" == 1 || "<c:out value='${assignPeriod}'/>" == 1){
			document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateMonthTarget&deptIds="+getDeptId();
		}else if("<c:out value='${businessTarget.assignPeriod}'/>" == 2 || "<c:out value='${assignPeriod}'/>" == 2){
			document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateSeasonTarget&deptIds="+getDeptId();
		}else if("<c:out value='${businessTarget.assignPeriod}'/>" == 3 || "<c:out value='${assignPeriod}'/>" == 3){
			document.getElementById("targetFrame").src = "./businessTarget.do?action=preUpdateYearTarget&deptIds="+getDeptId();
		}else{
			document.getElementById("targetFrame").src = "./businessTarget.do?action=emptyPage";
		}
	}
-->
</script>
</head>
<body onload="initPage()">
<form name="updateTargetForm" action="" method="post">
	<a class="common_title"><fmt:message key='target.title.base'/><fmt:message key='target.title.update'/></a>
	
<table cellpadding="0" cellspacing="0" class="table">
	<tr>
		<td>
			<div class="tools">
				<a class="save" href="javascript:save();" title="<fmt:message key='common.button.save'/>" hidefocus="true"><fmt:message key='common.button.save'/></a>&nbsp;&nbsp;&nbsp;&nbsp;
				<a class="back" href="javascript:back();" title="<fmt:message key='common.button.back'/>" hidefocus="true"><fmt:message key='common.button.back'/></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			</div>
		</td>
	</tr>
	<tr>
		<td>
			<table class="table_style">
				<tr>
					<td class="title3" width="15%"><fmt:message key='target.lable.type'/><fmt:message key='common.sign'/></td>
					<td class="title3">
						<fmt:message key="${businessTarget.assessmentTypeName}"/>
						<c:if test="${businessTarget.assessmentTypeValue != null}">${businessTarget.assessmentTypeValue}%</c:if>
						<c:if test="${businessTarget.assessmentTypeName2 != null}"><fmt:message key="${businessTarget.assessmentTypeName2}"/></c:if>
					</td>
					<td class="title3" width="15%"><fmt:message key='target.lable.assignPeriod'/><fmt:message key='common.sign'/></td>
					<td class="title3">
						<select id="assignPeriod" name="assignPeriod" onchange="displayTable(this)">
							<option value="0" <c:if test='${businessTarget.assignPeriod == 0 || assignPeriod == 0}'>selected</c:if> >--------<fmt:message key='common.select.option'/>--------</option>
							<option value="1" <c:if test='${businessTarget.assignPeriod == 1 || assignPeriod == 1}'>selected</c:if> ><fmt:message key='target.select.month'/></option>
							<option value="2" <c:if test='${businessTarget.assignPeriod == 2 || assignPeriod == 2}'>selected</c:if> ><fmt:message key='target.select.season'/></option>
							<option value="3" <c:if test='${businessTarget.assignPeriod == 3 || assignPeriod == 3}'>selected</c:if> ><fmt:message key='target.select.year'/></option>
						</select>
					</td>
				</tr>
				<tr>
					<td class="title3" width="15%"><fmt:message key='target.title.main'/><fmt:message key='common.sign'/></td>
					<td class="title3" width="40%">${businessTarget.totalTarget}<fmt:message key='common.lable.yuan'/></td>
					<td class="title3" width="15%">
						<fmt:message key='target.lable.assignDept'/><fmt:message key='common.sign'/>
						<input class="btn1" type="button" value="..." title="<fmt:message key='target.lable.selectDept'/>" 
							onclick="deptDialog()"/>
					</td>
					<td class="title3" width="" colspan="3">
						<%int i = 0; %>
						<c:forEach var="dept" items="${businessTarget.assignDepts}" varStatus="d">
							<c:if test="${dept.deptName != null && dept.deptName != ''}">
							${dept.deptName},<input id="deptId" name="deptId" type="hidden" value="${dept.id}"/>
							<%i = i + 1; %>
							<%if(i % 4 == 0){%><br><%}%>
							</c:if>
						</c:forEach>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<iframe id="targetFrame" name="targetFrame" src="./businessTarget.do?action=emptyPage" frameborder="0" width="100%" 
				onload="this.height=targetFrame.document.body.scrollHeight;" scrolling="no"></iframe>
		</td>
	</tr>
</table>
</form>
</body>
</html>